<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row :gutter="10">
          <!-- 搜索与添加区域 -->
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" :clearable="true">
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input>
          </el-col>
        </el-row>
      </div>

      <!-- 列表区域 -->
      <el-table :data="orderList" :stripe="true" style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="id"></el-table-column>
        <el-table-column label="商品名称" prop="name"></el-table-column>
        <el-table-column label="商品价格" prop="price"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="{row}">{{row.create_time | dateFormat}}</template>
        </el-table-column>

        <el-table-column label="操作" width="170">
          <template slot-scope="{row}">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editDialogShow(row.id)"
            ></el-button>

            <!-- 删除 -->
            <el-button
              type="warning"
              icon="fas fa-search-location"
              circle
              size="mini"
              @click="locationDialogShow(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="pageSizes"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 物流进度 -->
    <el-dialog title="物流进度" :visible.sync="locationDialogVisible" width="50%">
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(item, index) in orderLocation"
          :key="index"
          :timestamp="item.AcceptTime"
          placement="top"
        >{{item.AcceptStation}}</el-timeline-item>
      </el-timeline>

      <span slot="footer" class="dialog-footer">
        <el-button @click="locationDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFormOp">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
      <el-form :model="editFormData" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            placeholder="直接搜搜"
            v-model="editFormData.address1"
            :options="cityData"
            :props="defCityData"
            :filterable="true"
            :clearable="true"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editFormData.address2"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editFormOp">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import cityData from './city.data-3.js'
export default {
  data() {
    return {
      locationDialogVisible: false,
      editDialogVisible: false,
      // 分頁組件
      total: 0,
      pageSizes: [10, 15, 20],
      // 获取商品列表的参数 + 分页组件
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10,
      },
      orderList: [],
      editFormData: {
        address1: [],
        address2: '',
      },
      editFormRules: {
        address1: [
          { required: true, message: '请选择省市县/区', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData,
      defCityData: {
        label: 'text',
        expandTrigger: 'hover',
      },
      orderLocation: [],
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 新增表单显示
    async locationDialogShow(id) {
      await this.$http
        .get(`/kuaidi/{id}`)
        .then((res) => {
          res = res.data
          console.log(res)
          if (!res.code || res.code == 0)
            return this.$message.error(
              res.msg || window.defineGlobalConstData.getError
            )

          this.orderLocation = res.data

          this.locationDialogVisible = true
        })
        .catch((err) => {
          return this.$message.error(err.message)
        })
    },
    // 修改表单显示
    editDialogShow(id) {
      // console.log(id)
      this.editDialogVisible = true
    },
    // 新增表单操作
    addFormOp() {
      this.locationDialogVisible = false
    },
    // 修改表单操作
    editFormOp() {
      this.editDialogVisible = false
    },
    // 获取列表
    async getOrderList() {
      await this.$http
        .get('/orders', { params: this.queryInfo })
        .then((res) => {
          res = res.data
          // console.log(res)
          if (!res.code || res.code == 0)
            return this.$message.error(
              res.msg || window.defineGlobalConstData.getError
            )

          this.orderList = res.data.list
          this.total = res.data.total
        })
        .catch((err) => {
          return this.$message.error(err.message)
        })
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pageSize = val
      this.getOrderList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryInfo.pageNum = val
      this.getOrderList()
    },
    // 根据 id 删除记录
    async deleteById(id) {
      const confirm = await this.$confirm(
        window.defineGlobalConstData.delConfirm,
        window.defineGlobalConstData.delTitle,
        {
          confirmButtonText: window.defineGlobalConstData.delTrue,
          cancelButtonText: window.defineGlobalConstData.delFalse,
          type: 'warning',
        }
      ).catch((err) => err)

      if (confirm !== 'confirm' || confirm === 'cancel') {
        return this.$message.info(window.defineGlobalConstData.delCancel)
      }

      // todo 发起请求
    },
    editDialogClose() {
      this.$refs.editFormRef.resetFields()
    },
  },
  computed: {},
}
</script>

<style lang="scss" scoped>
.el-cascader {
  width: 100%;
}
</style>